﻿using CQRSLearn.Persistence;
using MediatR;

namespace CQRSLearn.Features.Products.Commands.Delete
{
    public class DeleteProductCommandHandler : IRequestHandler<DeleteProductCommand>
    {
        private readonly AppDbContext context;

        public DeleteProductCommandHandler(AppDbContext context)
        {
            this.context = context;
        }

        public async Task Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var product=await context.Products.FindAsync(request.Id);
            if (product == null)
            {
                return;
            }
            context.Products.Remove(product);
            await context.SaveChangesAsync();
        }
    }
}
